package demo02;

import com.sun.xml.internal.ws.wsdl.writer.document.Service;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TcpService2 {
    public static void main(String[] args) throws Exception {
        //创建服务
        ServerSocket serverSocket = new ServerSocket(9000);
        //监听客户端的连接
        Socket socket = serverSocket.accept();//阻滞式监听，会一直等待客户端
        //获取输入流
        InputStream is = socket.getInputStream();



        //文件输出
        FileOutputStream fos = new FileOutputStream(new File("网络编程/src/receive.png"));
        byte[] buffer =new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
            System.out.println("文件输出中........");
        };

        //通知客户端我接收完毕了
        OutputStream os1 = socket.getOutputStream();
        os1.write("我接收完毕，你可断开了".getBytes(StandardCharsets.UTF_8));

        //关闭资源
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();


    }















}
